package com.hunter.common.util;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;

/**
 * Velocity工具类
 * Created by hunter on 2017/05/03.
 */
public class VelocityUtil {

	/**
	 * 根据模板生成文件
	 * @param inputVmFilePath 模板路径
	 * @param outputFilePath 输出文件路径
	 * @param context
	 * @throws Exception
	 */
	public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception {
		try {
			Velocity.init();
			VelocityEngine engine = new VelocityEngine();
			Template template = engine.getTemplate(inputVmFilePath, "utf-8");
			File outputFile = new File(outputFilePath);
			FileWriter writer = new FileWriter(outputFile);
			template.merge(context, writer);
			writer.close();
		} catch (Exception ex) {
			throw ex;
		}
	}
	/**
	 * 根据模板生成文件
	 * 
	 * @param inputVmFileDir 模板文件夹
	 * @param inputVmFileName 模板文件名
	 * @param outputFilePath 输出文件路径
	 * @param context
	 * @throws Exception
	 */
	public static void generate1(String inputVmFileDir, String inputVmFileName, String outputFilePath, VelocityContext context) throws Exception {
		try {
			Velocity.init();
			VelocityEngine engine = new VelocityEngine();
			Properties properties = new Properties();
			properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, inputVmFileDir); //此处的fileDir可以直接用绝对路径来
			//指定,如"D:/template",但记住只要指定到文件夹就行了
			engine.init(properties);   //初始化
			Template template = engine.getTemplate(inputVmFileName, "utf-8");
			File outputFile = new File(outputFilePath);
			FileWriter writer = new FileWriter(outputFile);
			template.merge(context, writer);
			writer.close();
		} catch (Exception ex) {
			throw ex;
		}
	}
	/**
	 * 根据模板生成String
	 * 
	 * @param inputVmFilePath
	 *            模板路径
	 * @param context
	 * @throws Exception
	 */
	public static String toString(String inputVmFilePath, VelocityContext context) throws Exception {
		try {
			Velocity.init();
			VelocityEngine engine = new VelocityEngine();
			Template template = engine.getTemplate(inputVmFilePath, "utf-8");
			Writer writer = new StringWriter();
			template.merge(context, writer);
			return writer.toString();
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	/**
	 * 根据模板生成String
	 * 
	 * @param inputVmFileDir
	 *            模板文件夹
	 * @param inputVmFileName
	 *            模板文件名
	 * @param context
	 * @throws Exception
	 */
	public static String toString2(String inputVmFileDir, String inputVmFileName, VelocityContext context) throws Exception {
		try {
			Velocity.init();
			VelocityEngine engine = new VelocityEngine();
			Properties properties = new Properties();
			properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, inputVmFileDir); //此处的fileDir可以直接用绝对路径来
			//指定,如"D:/template",但记住只要指定到文件夹就行了
			engine.init(properties);   //初始化
			Template template = engine.getTemplate(inputVmFileName, "utf-8");
			Writer writer = new StringWriter();
			template.merge(context, writer);
			return writer.toString();
		} catch (Exception ex) {
			throw ex;
		}
	}
}
